layui.use(['layer', 'table', 'ax', 'func', 'form', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;

    /**
     * 单位类型管理
     */
    var ContactType = {
        tableId: "contactTypeTable"
    };

    /**
     * 初始化表格的列
     */
    ContactType.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'name', sort: true, title: '类型名称'},
            {field: 'createTime', sort: true, title: '创建时间', width: 160},
            {field: 'creatorName', sort: true, title: '创建人'},
            {field: 'updateTime', sort: true, title: '更新时间', width: 160},
            {field: 'updaterName', sort: true, title: '更新人'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ContactType.tableId,
        url: Changein.ctxPath + '/contactType/list',
        page: true,
        height: "full-130",
        cellMinWidth: 120,
        cols: ContactType.initColumn()
    });

    /**
     * 点击查询按钮
     */
    ContactType.search = function () {
        var queryData = {};
        queryData['name'] = $('#name').val();
        queryData['searchDate'] = $('#searchDate').val();
        table.reload(ContactType.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    ContactType.openAddDlg = function () {
        func.open({
            height: 300,
            title: '添加单位类型',
            content: Changein.ctxPath + '/contactType/add',
            tableId: ContactType.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ContactType.openEditDlg = function (data) {
        func.open({
            height: 300,
            title: '修改单位类型',
            content: Changein.ctxPath + '/contactType/edit?id=' + data.id,
            tableId: ContactType.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    ContactType.exportExcel = function () {
        var checkRows = table.checkStatus(ContactType.tableId);
        if (checkRows.data.length === 0) {
            Changein.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    var deleteLock = false;
    ContactType.onDeleteItem = function (data) {
        if (!deleteLock) {
            deleteLock = true;
            var operation = function () {
                var ajax = new $ax(Changein.ctxPath + "/contactType/delete", function (data) {
                    deleteLock = false;
                    if (data.code == 200) {
                        Changein.success("删除成功!");
                        table.reload(ContactType.tableId);
                    } else {
                        Changein.error("删除失败，" + data.message + "!");
                    }
                }, function (data) {
                    deleteLock = false;
                    Changein.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("id", data.id);
                ajax.start();
            };
            deleteLock = false;
            Changein.confirm("是否删除?", operation);
        } else {
            Changein.error("正在删除中");
        }
    };

    /**
     * 批量删除
     */
    var deleteListLock = false;
    ContactType.deleteList = function () {
        if (!deleteListLock) {
            deleteListLock = true;
            var checkRows = table.checkStatus(ContactType.tableId);
            if (checkRows.data.length == 0) {
                deleteListLock = false;
                Changein.error("请选择您要删除的数据");
                return;
            }
            var operation = function () {
                let contactTypeIds = [];
                let list = checkRows.data;
                for (let i = 0; i < list.length; i++) {
                    contactTypeIds.push(list[i].id);
                }
                // 执行请求
                $.ajax({
                    type: "POST",
                    url: Changein.ctxPath + "/contactType/deleteList",
                    data: {
                        contactTypeIds: contactTypeIds
                    },
                    traditional: true,
                    success: function (data) {
                        deleteListLock = false;
                        if (data.code == 200) {
                            Changein.success("删除成功!");
                        } else {
                            Changein.error("删除失败，" + data.message);
                        }
                        table.reload(ContactType.tableId);
                    }
                });
            };
            deleteListLock = false;
            Changein.confirm("是否删除?", operation);
        }
    };

    // 执行一个laydate实例
    laydate.render({
        elem: '#searchDate' //指定元素
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ContactType.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ContactType.openAddDlg();
    });

    // 批量删除按钮点击事件
    $('#btnDel').click(function () {
        ContactType.deleteList();
    });

    // 导出excel
    $('#btnExp').click(function () {
        var tableLen =  table.cache.contactTypeTable.length;
        if (tableLen == 0){
            Changein.error("当前暂无数据");
            return false;
        }
        ContactType.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ContactType.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            ContactType.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ContactType.onDeleteItem(data);
        }
    });

});
